package com.hbcj.dlts.controller;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.*;
import java.util.*;

@RequestMapping("/fm")
@Controller
public class FreeMarkerController {

    private Logger Log = LoggerFactory.getLogger(FreeMarkerController.class);

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Autowired
    private Configuration configuration;

    @Value("${pagedir}")
    private String pagedir;

    @GetMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("hello", "<h1>Hello FreeMarker"+pagedir+"</h1>");
        return "hello";
    }


    @RequestMapping("/genHtml")
    @ResponseBody
    public void test1()  {
        try {
            //获取模板
            Template template = configuration.getTemplate("hello.ftl");
            //数据
            Map map = new HashMap();
            map.put("name","李四");
            //输出位置
            Writer writer = new FileWriter(pagedir+6688+".html");
            Log.info("填充后模板输出的位置 : {}",pagedir);
            //填充模板
            template.process(map,writer);
            //关闭流
            writer.close();
        } catch (Exception e) {
            Log.error("模板填充失败!");
        }
        Log.info("模板填充成功!");

    }





}
